package com.tanglongan.concurrent._sync;

import lombok.AllArgsConstructor;

public class Sync06 {
    public static void main(String[] args) {
        Sync06ServiceB service = new Sync06ServiceB();
        Thread t1 = new Sync06Thread(service);
        Thread t2 = new Sync06Thread(service);
        t1.start();
        t2.start();
    }
}

class Sync06ServiceA {
    synchronized public void foo() {
        try {
            System.out.println("父类：" + Thread.currentThread().getName() + ", 开始于" + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("父类：" + Thread.currentThread().getName() + ", 结束于" + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Sync06ServiceB extends Sync06ServiceA {
    public void foo() {
        try {
            System.out.println("子类：" + Thread.currentThread().getName() + ", 开始于" + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("子类：" + Thread.currentThread().getName() + ", 结束于" + System.currentTimeMillis());
            super.foo();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


@AllArgsConstructor
class Sync06Thread extends Thread {
    private Sync06ServiceA service;

    @Override
    public void run() {
        service.foo();
    }
}